package com.mvc.web.user.dao.impl;

import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Repository;

import com.mvc.web.common.ibatis.IbatisDaoImpl;
import com.mvc.web.common.utils.MD5Util;
import com.mvc.web.user.dao.UserDao;
import com.mvc.web.user.vo.LoginForm;
import com.mvc.web.user.vo.UserVo;

@Repository
public class UserDaoImpl extends IbatisDaoImpl implements UserDao{

	@Override
	public UserVo getUser(LoginForm vo) {
		return (UserVo) queryForObject("user.getUser", vo);
	}

	@Override
	public String getCountByPhone(String phone) {
		return (String) queryForObject("user.getCountByPhone", phone);
	}

	@Override
	public void createUser(String phone, int verifyCode, long tokenTime) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("phone", phone);
		map.put("verifyCode", verifyCode);
		map.put("tokenTime", tokenTime);
		map.put("name", UUID.randomUUID().toString().substring(0, 8));
		map.put("ctime", System.currentTimeMillis()/1000);
		insert("user.createUser", map);
	}

	@Override
	public void updateUser(String phone, int verifyCode, long tokenTime) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("phone", phone);
		map.put("verifyCode", verifyCode);
		map.put("tokenTime", tokenTime);
		update("user.updateUser", map);
	}

	@Override
	public void uploadImage(int uid, String imageUrl) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("uid", uid);
		map.put("imageUrl", imageUrl);
		update("user.uploadImage", map);
	}

	@Override
	public void setPwd(int uid, String newPwd) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("uid", uid);
		map.put("newPwd", MD5Util.string2MD5(MD5Util.string2MD5(newPwd)));
		update("user.setPwd", map);
	}

	@Override
	public int createUser(String carOwnerPhone, String carOwnerName) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("phone", carOwnerPhone);
		map.put("verifycode", 0);
		map.put("tokenTime", System.currentTimeMillis()/1000);
		map.put("name", carOwnerName);
		map.put("ctime", System.currentTimeMillis()/1000);
		return (int) insert("user.createUser", map);
	}

	@Override
	public void updateUserName(String carOwnerPhone, String carOwnerName) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("phone", carOwnerPhone);
		map.put("name", carOwnerName);
		update("user.updateUserName",map);
	}
}
